"""
Copyright 2008 Olivier Belanger

This file is part of Ounk.

Ounk is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Ounk is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Ounk.  If not, see <http://www.gnu.org/licenses/>.
"""

import threading, time
from types import ListType

class Liner(threading.Thread):
    def __init__(self, start, stop, duration):
        threading.Thread.__init__(self)
        self.poll = .02
        self.startv = start
        self.stopv = stop
        if type(self.stopv) != ListType:
            self.stopv = [self.stopv]
        self.time = duration
        if type(self.time) != ListType:
            self.time = [self.time]
        self.length = len(self.stopv)    
        self.currentVal = self.startv
        if self.startv <= self.stopv[0]: 
            self.dir = 'up'
        else: 
            self.dir = 'down'
        self.nstep = int(self.time[0] / self.poll)
        self.step = (float(max([self.startv, self.stopv[0]])) - float(min([self.startv, self.stopv[0]]))) / self.nstep
        self.currentStep = 0
        self.npass = 0
        self.terminated = False
        self.ok = False
        
    def run(self):
        while not self.terminated:
            if self.ok:
                if self.dir == 'up':
                    self.currentVal += self.step
                else:
                    self.currentVal -= self.step
                self.currentStep += 1
                if self.currentStep >= self.nstep:
                    self.npass += 1
                    if self.npass >= self.length:
                        self.ok = False
                    else:
                        self.initNewPass(self.npass)                        
            time.sleep(self.poll)
        print "thread terminated"    

    def initNewPass(self, npass):
        if self.currentVal <= self.stopv[npass]: 
            self.dir = 'up'
        else: 
            self.dir = 'down'
        self.nstep = int(self.time[npass % len(self.time)] / self.poll)
        self.step = (float(max([self.currentVal, self.stopv[npass]])) - float(min([self.currentVal, self.stopv[npass]]))) / self.nstep
        self.currentStep = 0
        
    def getValue(self):
        """Returns the current value of a line object.

--- PARAMETERS ---

no parameter

* See line page for an example.
"""
        return self.currentVal
        
    def play(self, state=True):
        self.ok = state
        if state == True:
            self.currentVal = self.startv
            if self.startv <= self.stopv[0]: 
                self.dir = 'up'
            else: 
                self.dir = 'down'
            self.nstep = int(self.time[0] / self.poll)
            self.step = (float(max([self.startv, self.stopv[0]])) - float(min([self.startv, self.stopv[0]]))) / self.nstep
            self.currentStep = 0
            self.npass = 0
            
    def stop(self):
        self.terminated = True

